package com.example.myapplication.Entity;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.example.myapplication.Coder.Util;

import org.msgpack.annotation.Index;
import org.msgpack.annotation.Message;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * 消息实体类，协议
 */
@Message  // 为了防止消息无法被序列化编码
public class ChatMessage {
    private static final String TAG = "ChatMessage";
    @Index(0)  // @Index()标签表明数据的顺序
    private String sendUser;
    @Index(1)
    private String receiveUser;
    @Index(2)
    private String message;
    @Index(3)
    private int messagetype;//1:初始化认证消息，2：聊天消息 // 3.添加好友请求 // 4.同意好友添加请求 // 5.拒绝好友添加请求

    public ChatMessage() { // 空参构造
    }

    public ChatMessage(String sendUser, String receiveUser, String message, int messagetype){
        this.sendUser=sendUser;
        this.receiveUser=receiveUser;
        this.message=message;
        this.messagetype=messagetype;
    }

    /**
     * 获得消息的发送者
     * @return 返回消息的发送者
     */
    public String getSendUser() {
        return sendUser;
    }

    /**
     * 设置要发送消息的对象
     * @param sendUser 发送者
     */
    public void setSendUser(String sendUser) {
        this.sendUser = sendUser;
    }

    /**
     * 获得消息的接受者
     * @return 返回消息的接受者
     */
    public String getReceiveUser() {
        return receiveUser;
    }

    /**
     * 设置消息的接受者
     * @param receiveUser 接受者
     */
    public void setReceiveUser(String receiveUser) {
        this.receiveUser = receiveUser;
    }

    /**
     * 得到消息内容
     * @return 消息的内容
     */
    public String getMessage() {
        return message;
    }

    /**
     * 设置要发送的消息内容
     * @param message 消息的内容
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 获得发送消息的种类
     * @return 返回消息的种类
     */
    public int getMessagetype() {
        return messagetype;
    }

    /**
     * 设置发送消息的类型
     * 1:初始化认证消息，2：聊天消息
     * @param messagetype 消息的类型
     */
    public void setMessagetype(int messagetype) {
        this.messagetype = messagetype;
    }

    @Override
    public String toString() {
        return "ChatMessage{" +
                "sendUser='" + sendUser + '\'' +
                ", receiveUser='" + receiveUser + '\'' +
                ", message='" + message + '\'' +
                ", messageType='" + messagetype + '\'' +
                '}';
    }
    public byte[] encode() throws UnsupportedEncodingException {
        int sendUserLength = sendUser.getBytes("UTF-8").length;
        int receiveUserLength = receiveUser.getBytes("UTF-8").length;
        int messageLength = message.getBytes("UTF-8").length;
        int totalLength = 4 + sendUserLength + 4 + receiveUserLength + 4 + messageLength + 4;
        byte[] buffer = new byte[totalLength];
        int offset = 0;

        // 前四个字节存储sendUser的长度
        System.arraycopy(Util.int2bytes(sendUserLength), 0, buffer, offset, 4);
        offset += 4;
        // copy send user to buffer
        System.arraycopy(sendUser.getBytes("UTF-8"), 0, buffer, offset, sendUserLength);
        offset += sendUserLength;

        System.arraycopy(Util.int2bytes(receiveUserLength), 0, buffer, offset, 4);
        offset += 4;

        System.arraycopy(receiveUser.getBytes("UTF-8"), 0, buffer, offset, receiveUserLength );
        offset += receiveUserLength;

        System.arraycopy(Util.int2bytes(messageLength), 0, buffer, offset, 4);
        offset += 4;

        System.arraycopy(message.getBytes("UTF-8"), 0, buffer, offset, messageLength);
        offset += messageLength;
        // copy sign to buffer

        byte[] messageTypeBuffer = Util.int2bytes(messagetype);
        System.arraycopy(messageTypeBuffer, 0, buffer, offset, 4);

        return buffer;
    }
    public void decode(byte[] buffer) throws UnsupportedEncodingException {
        int start_pos = 0;
        // decode send user

        // 获取长度
        int length = Util.bytes2int(buffer, start_pos);
        start_pos += 4;
        this.sendUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;
        System.out.println(start_pos);

        length = Util.bytes2int(buffer, start_pos);
        start_pos += 4;
        this.receiveUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;
        System.out.println(start_pos);

        length = Util.bytes2int(buffer, start_pos);
        start_pos += 4;
        this.message = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;
        System.out.println(start_pos);

        this.messagetype = Util.bytes2int(buffer, start_pos);
        System.out.println();
    }

}